// packageK/lovewhite/withdrawalList/withdrawalList.js
var o = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    language: "",
    page: 1,
    total: 0,
    list: [],
    uid: "",
    isLoding: false,
    shopId:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var t = wx.getStorageSync("langIndex"),
      u = wx.getStorageSync('uid');
    this.setData({
      language: t.en,
      uid: u
    });
    this.getList()
    this.getshopId()
  },
  getList() {
    let that = this
    var t = o.getNetAddresss("FeiEr.get_user_withdraw");
    o._postNetWork({
      url: t,
      data: {
        uid: that.data.uid,
        page: that.data.page,
        row: 10
      },
      success(l) {
        let i = l.data
        if (i.result == 1) {
          let a = i.data.data.map(item => {
            item['time'] = that.timeStamp(item.created_at*1000) 
            return item
          })
          let d = [...that.data.list, ...a]
          that.setData({
            total: i.data.total,
            list: d,
            isLoding: false
          })
        }
      }
    })
  },
  // 时间转换
  timeStamp(time) {
    var data = new Date(time);
    year = data.getFullYear() + '-'; //获取完整年份  4位
    month = (data.getMonth() + 1 < 10 ? '0' + (data.getMonth() + 1) : data.getMonth() + 1) + '-'; //获取月份（0-11） 如果月份小于10显示为01，02
    day = data.getDate() < 10 ? '0' + data.getDate() : data.getDate() + ' '; //获取日（1-31）  小于10为01，02
    hours = (data.getHours() < 10 ? '0' + data.getHours() : data.getHours()) + ':'; //获取小时（0-23）
    minutes = (data.getMinutes() < 10 ? '0' + data.getMinutes() : data.getMinutes()) + ':'; //获取分钟（0-59） 
    secondes = data.getSeconds() < 10 ? '0' + data.getSeconds() : data.getSeconds(); //获取秒（0-59）
    return year + month + day + hours + minutes + secondes
  },
  // 提现
  withdrawal(e){
    let data = e.currentTarget.dataset.item
    let that = this
    wx.showModal({
      content: '确认要领取吗',
      complete: (res) => {
        if (res.cancel) {
          
        }
    
        if (res.confirm) {
          if (wx.canIUse('requestMerchantTransfer')) {
            wx.requestMerchantTransfer({
              mchId: that.data.shopId,
              appId: wx.getAccountInfoSync().miniProgram.appId,
              package: data.package_info,
              success: (res) => {
                // res.err_msg将在页面展示成功后返回应用时返回ok，并不代表付款成功
                console.log('success:', res);
                that.upStatus(data.id)
              },
              fail: (res) => {
                console.log('fail:', res);
              },
            });
          } else {
            wx.showModal({
              content: '你的微信版本过低，请更新至最新版本。',
              showCancel: false,
            });
          }
        }
      }
    })
    console.log(data);
  },
  getshopId(){
    let that = this
    var t = o.getNetAddresss("FeiEr.get_mchId");
    o._postNetWork({
      url: t,
      success(l) {
        let i = l.data
        if (i.result == 1) {
          that.setData({
            shopId:i.data
          })
        }
      }
    })
  },
  // 领取后修改状态
  upStatus(id){
    let that = this
    var t = o.getNetAddresss("FeiEr.user_ling");
    o._postNetWork({
      url: t,
      data:{
        id:id
      },
      success(l) {
        let i = l.data
        if (i.result == 1) {
          let arr = that.data.list.map(item=>{
            if(item.id == id){
              item.lq_status = 1
            }
            return item
          })
          that.setData({
            list:arr
          })
          
        }
      }
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

    if (this.data.list.length < this.data.total && !this.data.isLoding) {
      this.setData({
        isLoding: true
      })
      this.data.page++
      this.getList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})